/**
 * Copyright &copy; 2012-2016 <a href="https://git.oschina.net/zdw2016/webbase5">Webbase</a> All rights reserved.
 */
package com.foreveross.webbase.projectdemo.web;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.foreveross.webbase.common.config.Global;
import com.foreveross.webbase.common.persistence.Page;
import com.foreveross.webbase.common.web.BaseController;
import com.foreveross.webbase.common.utils.StringUtils;
import com.foreveross.webbase.projectdemo.entity.Servertype;
import com.foreveross.webbase.projectdemo.service.ServertypeService;

/**
 * 服务器类型管理Controller
 * @author yangwanli
 * @version 2017-03-15
 */
@Controller
@RequestMapping(value = "${adminPath}/projectdemo/servertype")
public class ServertypeController extends BaseController {

	@Autowired
	private ServertypeService servertypeService;
	
	@RequiresPermissions("projectdemo:servertype:view")
	@RequestMapping(value = {"list", ""})
	public String list(Servertype servertype) {
		Page<Servertype> page = servertypeService.findPage(new Page<Servertype>(request(), response()), servertype); 
		attr("page", page);
		return "projectdemo/servertypeList";
	}

	@RequiresPermissions("projectdemo:servertype:view")
	@RequestMapping("form")
	public String form(String id) {
		if(StringUtils.isNotEmpty(id)) {
			Servertype servertype=servertypeService.get(id);
			attr("servertype", servertype);
		} else {
			attr("servertype", new Servertype());
		}
		return "projectdemo/servertypeForm";
	}

	@RequiresPermissions("projectdemo:servertype:edit")
	@RequestMapping("save")
	public String save(Servertype servertype, RedirectAttributes redirectAttributes) {
		if (!beanValidator(redirectAttributes, servertype)){
			return form(servertype.getId());
		}
		servertypeService.save(servertype);
		addMessage(redirectAttributes, "保存服务器类型成功");
		return "redirect:"+Global.getAdminPath()+"/projectdemo/servertype/?repage";
	}
	
	@RequiresPermissions("projectdemo:servertype:edit")
	@RequestMapping("delete")
	public String delete(@RequestParam String id, RedirectAttributes redirectAttributes) {
		servertypeService.delete(id);
		addMessage(redirectAttributes, "删除服务器类型成功");
		return "redirect:"+Global.getAdminPath()+"/projectdemo/servertype/?repage";
	}

}